﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_ManageUser : System.Web.UI.Page
{
    DAL.DALRole dr = new DAL.DALRole();

    string errMsg = "";
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.oldpwd.Visible = false;
            this.btnEdit.Visible = false;
            this.btnSure.Visible = true;
            dropRoleSelect.DataTextField = "UGROUP";
            dropRoleSelect.DataValueField = "UGROUP";
            dropRoleSelect.DataSource = dr.GetAllRole(out errMsg);
            dropRoleSelect.DataBind();
            gridUserList.DataSource = dr.GetUsersByRoleName(dropRoleSelect.SelectedValue.ToString(),out errMsg);
            gridUserList.DataBind();
        }
    }

    protected void dropRoleSelect_SelectedIndexChanged(object sender, EventArgs e)
    {
        gridUserList.DataSource = dr.GetUsersByRoleName(dropRoleSelect.SelectedValue.ToString(), out errMsg);
        gridUserList.DataBind();
    }

    protected void btnSure_Click(object sender, EventArgs e)
    {
        string message = "";
        string rolename = dropRoleSelect.SelectedValue.ToString();
        string username = this.textUserName.Text;
        string pwd = this.textNewPwd.Text;
        string pwds = this.textSurePwd.Text;
        this.textUserName.Text = "";
        if (this.textOldPwd.Text == "")
        {
            if (pwd == pwds)
            {
                if (dr.InsertUser(username, pwd, rolename, out errMsg))
                {
                    message = "添加用户成功！";
                    gridUserList.DataSource = dr.GetUsersByRoleName(dropRoleSelect.SelectedValue.ToString(), out errMsg);
                    gridUserList.DataBind();
                }
                else
                {
                    message = "添加用户失败，请检查该用户是否已经存在！";
                }
            }
        }
        this.oldpwd.Visible = false;
        Response.Write("<script>alert('" + message + "')</script>");

    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        textUserName.Text = "";
        this.oldpwd.Visible = false;
        this.btnEdit.Visible = false;
        this.btnSure.Visible = true;
    }

    protected void gridUserList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string userid = gridUserList.DataKeys[e.RowIndex].Value.ToString();
        string message = "";
        if (dr.DeleteUserById(userid, out errMsg))
        {
            gridUserList.DataSource = dr.GetUsersByRoleName(dropRoleSelect.SelectedValue.ToString(), out errMsg);
            gridUserList.DataBind();
            message = "删除用户成功！";
        }
        else
        {
            message = "删除失败，请重新尝试！";
        }
        Response.Write("<script>alert('" + message + "')</script>");
    }

    protected void gridUserList_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        this.oldpwd.Visible = true;
        this.btnEdit.Visible = true;
        this.btnSure.Visible = false;
        string idkey = gridUserList.DataKeys[e.NewSelectedIndex].Value.ToString();
        int id = int.Parse(idkey);
        this.textUserName.Text = gridUserList.Rows[e.NewSelectedIndex].Cells[1].Text;
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        string message = "";
        string Opwd = this.textOldPwd.Text;
        string rolename = this.dropRoleSelect.SelectedValue.ToString();
        string username = this.textUserName.Text;
        string Npwd = this.textNewPwd.Text;
        this.textUserName.Text = "";
        string oldpwd = dr.GetOldPwd(username, rolename, out errMsg);
        if (oldpwd == Opwd)
        {
            if (dr.EditPwd(username, rolename, Npwd, out errMsg))
            {
                message = "密码修改成功！";
                gridUserList.DataSource = dr.GetUsersByRoleName(dropRoleSelect.SelectedValue.ToString(), out errMsg);
                gridUserList.DataBind();
            }
            else
            {
                message = "密码修改失败，请检查密码格式是否正确！";
            }
        }
        else
        {
            message = "旧密码不正确，请重新输入正确的旧密码，忘记旧密码请联系系统管理员！";
        }
        Response.Write("<script>alert('" + message + "')</script>");
        this.oldpwd.Visible = false;
        this.btnEdit.Visible = false;
        this.btnSure.Visible = true;
    }
}
